//
//  TableCellItem.swift
//  TSKit
//
//  Created by chenyusen on 2018/3/16.
//

import UIKit

open class TableCellItem: NSObject {
    
    /// 当前cellItem所绑定的数据
    open var model: Any?
    
    /// 当前cellItem所绑定的UITableViewCell
    public weak var currentCell: UITableViewCell?
    
    /// 当前cellItem所绑定的UITableViewCell的类
    open var cellClass: UITableViewCell.Type? {
        return UITableViewCell.self
    }
    
    /// 当前cellItem所绑定的UITableViewCell的xib
    open var cellNib: UINib? {
        return nil
    }
    
    /// 当前所绑定UITableViewCell的高度
    open var cellHeight: CGFloat?
    
    
    /// 支持的侧滑
    open var rowActions: [UITableViewRowAction]? {
        return nil
    }
    
    public required init(attributes:[AnyHashable: Any]? = nil) {
        super.init()
    }
}


open class TableCell: UITableViewCell {
    
    
    /// 当前cell所持有的cellItem
    public var cellItem: TableCellItem?
    
    required public override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open class var shouldAppendCellItemClassToReuseIdentifier: Bool {
        return false
    }
    
    /// 更新数据
    ///
    /// - Parameter cellItem: 更新数据
    open func updateCell(_ cellItem: TableCellItem?) {
        self.cellItem = cellItem
        cellItem?.currentCell = self
    }
    
    
    open override func prepareForReuse() {
        if cellItem?.currentCell == self {
            cellItem?.currentCell = nil
        }
        cellItem = nil
        super.prepareForReuse()
    }
}
